-- the local upvalues bandwagon
local UnitIsDead = UnitIsDead
local UnitIsGhost = UnitIsGhost
local UnitIsDeadOrGhost = UnitIsDeadOrGhost
local UnitLevel = UnitLevel
local UnitPowerType = UnitPowerType

local core = oUF.Jerry

local backdrop = core.backdrop
local statusbartexture = core.statusbartexture
-- colors
local powercolor = core.powercolor
local OnEnter, OnLeave = core.OnEnter, core.OnLeave
local getFontString = core.getFontString
local styleClicks = core.styleClicks
local styleCombatFeedback = core.styleCombatFeedback

-- This is the core of RightClick menus on diffrent frames

local function updateName(self, event, unit)
	if self.unit ~= unit then return end

	self.Name:SetText(UnitName(unit))
	self.Lvl:SetText(UnitLevel(unit))
end

local classColor = RAID_CLASS_COLORS[select(2, UnitClass("player"))]

local function updateHealth(self, event, bar, unit, min, max)
	if UnitIsDead(unit) then
		bar:SetValue(0)
		bar.value:SetText("Dead")
	elseif UnitIsGhost(unit) then
		bar:SetValue(0)
		bar.value:SetText("Ghost")
	else
		local deficit = min - max
		if deficit == 0 then
			bar.value:SetFormattedText("%d/%d", min, max )
		else
			bar.value:SetFormattedText("%d/%d |cffff8080%d|r", min, max, deficit)
		end
	end
	local r, g, b = classColor.r, classColor.g, classColor.b
	bar:SetStatusBarColor(r, g, b)
	bar.bg:SetTexture(r, g, b, .25)
end

local function updatePower(self, event, bar, unit, min, max)
	if max == 0 or UnitIsDeadOrGhost(unit) then
		bar:SetValue(0)
		bar.value:SetText()
	else
		bar.value:SetFormattedText("%d/%d", min, max)
	end

	local color = powercolor(unit)
	bar:SetStatusBarColor(color.r, color.g, color.b)
	bar.bg:SetTexture(color.r, color.g, color.b, 0.25)
end

local function setStyle(settings, self, unit)
	styleClicks(self, PlayerFrameDropDown)

	-- Background
	self:SetBackdrop(backdrop)
	self:SetBackdropColor(0,0,0,1)

	-- Healthbar
	local hp = CreateFrame("StatusBar", nil, self)
	hp:SetHeight(22)
	hp:SetStatusBarTexture(statusbartexture)
	hp:SetPoint("TOPLEFT")
	hp:SetPoint("TOPRIGHT")

	-- Healthbar background
	hp.bg = hp:CreateTexture(nil, "BORDER")
	hp.bg:SetAllPoints(hp)
	hp.bg:SetTexture(.25,.25,.25)

	-- Healthbar text
	hp.value = getFontString(hp)
	hp.value:SetPoint("RIGHT", -2, -1)

	self.Health = hp
	self.OverrideUpdateHealth = updateHealth

	local icon = hp:CreateTexture(nil, "OVERLAY")
	icon:SetHeight(16)
	icon:SetWidth(16)
	icon:SetPoint("TOP", self, 0, 8)
	icon:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons")
	self.RaidIcon = icon

	self.Name = getFontString(hp)
	self.Name:SetPoint("LEFT", 2, -1)
	self.UNIT_NAME_UPDATE = updateName

	-- Power Bar
	local pp = CreateFrame("StatusBar", nil, self)
	pp:SetHeight(16)
	pp:SetStatusBarTexture(statusbartexture)

	pp:SetPoint("LEFT")
	pp:SetPoint("RIGHT")
	pp:SetPoint("TOP", hp, "BOTTOM", 0, -1.35)

	local ppbg = pp:CreateTexture(nil, "BORDER")
	ppbg:SetAllPoints(pp)
	ppbg:SetTexture(.25,.25,.25)

	pp.bg = ppbg
	self.Power = pp
	self.OverrideUpdatePower = updatePower

	pp.value = getFontString(pp)
	pp.value:SetPoint("RIGHT", -2, 1)

	self.Lvl = getFontString(pp)
	self.Lvl:SetPoint("LEFT", pp, "LEFT", 2, 0)
	self.Lvl:SetTextColor(1.0, 1.0, 0.0)

	self.Class = getFontString(pp)
	self.Class:SetPoint("LEFT", self.Lvl, "RIGHT",  1, 0)

	self.Race = getFontString(pp)
	self.Race:SetPoint("LEFT", self.Class, "RIGHT",  1, 0)

	local leader = hp:CreateTexture(nil, "OVERLAY")
	leader:SetHeight(16)
	leader:SetWidth(16)
	leader:SetPoint("TOPLEFT", self, 0, 8)
	leader:SetTexture("Interface\\GroupFrame\\UI-Group-LeaderIcon")
	self.Leader = leader

	local combat = hp:CreateTexture(nil, "OVERLAY")
	combat:SetHeight(16)
	combat:SetWidth(16)
	combat:SetPoint("LEFT", leader, "RIGHT", 2, 0)
	combat:SetTexture("Interface\\CharacterFrame\\UI-StateIcon")
	combat:SetTexCoord(0.57, 0.90, 0.08, 0.41)
	self.Combat = combat

	local resting = hp:CreateTexture(nil, "OVERLAY")
	resting:SetHeight(16)
	resting:SetWidth(16)
	resting:SetPoint("LEFT", combat, "RIGHT", 2, 0)
	resting:SetTexture("Interface\\CharacterFrame\\UI-StateIcon")
	resting:SetTexCoord(0.09, 0.43, 0.08, 0.42)
	self.Resting = resting

	self.DebuffHighlightBackdrop = true -- oUF_DebuffHighlight Support, using the backdrop
	self.DebuffHighlightFilter = true -- only show debuffs I can cure

	styleCombatFeedback(self)

	self.Class:SetText(UnitClass("player"))
	self.Class:SetVertexColor(classColor.r, classColor.g, classColor.b)
	self.Race:SetText(UnitRace("player"))

	return self
end

local player = oUF:Spawn("player", "oUF_Player", {
	["initial-width"] = 200,
	["initial-height"] = 39,
	setup = setStyle,
})
player:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 20, -60)
